/**
 * @param {number[]} nums
 * @param {number[]} index
 * @return {number[]}
*/
var createTargetArray = function(nums, index) {
  let result = [];
  for(let i = 0; i < index.length; i++) {
    if(index[i] >= result.length) {
      result.push(nums[i]);
    } else {
      for(let j = result.length; j >= 1; j--) {
        result[j] = result[j - 1];
        if(j - 1 == index[i]) {
          result[j - 1] = nums[i];
          break;
        }
      }
    }
  }
  return result;
};
let nums = [0,1,2,3,4], index = [0,1,2,2,1];
console.log(createTargetArray(nums, index));
nums = [1,2,3,4,0], index = [0,1,2,3,0];
console.log(createTargetArray(nums, index));
nums = [1], index = [0];
console.log(createTargetArray(nums, index));
